
//
// 20120714 - (AH) - (Kryddar) Created.
// 20120922 - (AH) - (Midtun) Captors(Captor...).

package org.soekrat.engine.captor;

import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.soekrat.Log;
import org.soekrat.engine.captor.event.CaptorListener;


/**
 * <p>
 *   Pool of
 *   {@link Captor}s.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class Captors implements Iterable<Captor> 
{
  private final List<Captor> m_pool=new ArrayList<Captor>();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public Captors(){}
  
  /**
   * <p>
   *   Constructors.
   * </p>
   * @param captors {@link Captor}s.
   */
  public Captors(Captor... captors)
  {
    if (captors!=null)
    {
      for (Captor captor : captors)
      {
        if (captor!=null) addCaptor(captor);  
      }
    }
  }
  
  /**
   * <p>
   *   Gets numbers of captors.
   * </p>
   * @return Count.
   */
  public int size(){ return m_pool.size(); }
  
  /**
   * <p>
   *   Tells if is empty or not.
   * </p>
   * @return {@code size()==0}.
   */
  public boolean isEmpty(){ return size()==0; } 
  
  /**
   * <p>
   *   Adds
   *   {@link Captor}.
   * </p>
   * @param captor {@code Captor} to add.
   */
  public void addCaptor(Captor captor)
  {
    if (captor==null)
    {
      throw new NullPointerException("captor");
    }
    
    if (!m_pool.contains(captor)) m_pool.add(captor);
  }

  // Defined in Iterable<Captor>.
  public Iterator<Captor> iterator(){ return m_pool.iterator(); }
  
  /**
   * <p>
   *   Gets 
   *   {@link Captor}s.
   * </p>
   * @return {@code Captor}s.
   */
  public Captor[] getCaptors(){ return m_pool.toArray(Captor.EMPTY_ARRAY); }
  
  /**
   * <p>
   *   Adds 
   *   {@link CaptorListener} to 
   *   {@link Captor}s.
   * </p>
   * @param l Listener to add.
   */
  public void addCaptorListener(CaptorListener l)
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    
    Log.log(c_log, this, "addCaptorListener", "Adds listener: "+
      l.getClass().getSimpleName()+" to captors:");
    
    for (Captor captor : this) 
    {
      Log.log(c_log, this, "addCaptorListener", "  "+
        captor.getClass().getSimpleName());
          
      captor.addListener(l);
    }
  }
  
  /**
   * <p>
   *   Removes 
   *   {@link CaptorListener} to 
   *   {@link Captor}s.
   * </p>
   * @param l Listener to remove.
   */
  public void removeCaptorListener(CaptorListener l)
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    
    for (Captor captor : this) captor.removeListener(l);
  }
  
  @Override
  public String toString()
  {
    CharArrayWriter caw=new CharArrayWriter();
    PrintWriter pw=new PrintWriter(caw);
    
    for (Captor captor : this) pw.println(captor.getClass().getName());
    
    pw.flush();
    return caw.toString();
  }
  
  private static final boolean c_log=true;
  
}
